﻿using Autofac;
using Autofac.Integration.Mvc;
using AutoMapper;
using GiaSu.Business.Business.Implement;
using GiaSu.Bussiness.Business;
using GiaSu.Bussiness.Business.Implement;
using GiaSu.Bussiness.DIConfigure;
using GiaSu.DataAccess.Entity;
using GiaSu.WebApplication.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace GiaSu.WebApplication
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            StartMapping();
            ConfigureIoC();
        }
        private void ConfigureIoC()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(Assembly.Load("GiaSu.Business"))

                    .Where(t => t.Name.EndsWith("BusinessImpl"))

                    .AsImplementedInterfaces()

                    .InstancePerLifetimeScope();

            builder.RegisterControllers(Assembly.GetExecutingAssembly());
          
            RegisterIoC.Configure(builder);
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
        private void StartMapping()
        {
            Mapper.CreateMap<Parent, ParentAdd>();
            Mapper.CreateMap<ParentAdd, Parent>();
            Mapper.CreateMap<Parent, ParentView>();
            Mapper.CreateMap<Parent, ParentView>().ForMember(x => x.GradeName, m => m.MapFrom(p => p.Grade.Name))
                .ForMember(x => x.SubjectName, m => m.MapFrom(p => p.Subject.Name));

        }
    }
}
